﻿/*
输入：
6 1
2 2 3 1 5 2
2 3 1 5 4 3
输出：
5
*/
// 物品种类M，背包容量N
string MN = Console.ReadLine();
string[] MNArr = MN.Split(" ");
int M = int.Parse(MNArr[0]);
int N = int.Parse(MNArr[1]);

// 各个物体所占的空间SpaceList，各个物体的价值ValueList
string SpaceStr = Console.ReadLine();
string ValueStr = Console.ReadLine();
string[] SpaceStrArr = SpaceStr.Split(" ");
string[] ValueStrArr = ValueStr.Split(" ");
int[] SpaceList = new int[M];
int[] ValueList = new int[M];
for (int i = 0; i < M; i++)
{
    SpaceList[i] = int.Parse(SpaceStrArr[i]);
    ValueList[i] = int.Parse(ValueStrArr[i]);
}

// 二维数组处理01背包问题
int Array2DKnapsack(int M, int N, int[] SpaceList, int[] ValueList)
{
    int[,] dp = new int[M, N+1];
    for (int i = 0; i < M; i++)
    {
        for (int j = 0; j < N+1; j++)
        {
            if (i == 0)
            {
                dp[i, j] = j < SpaceList[i] ? 0 : ValueList[i];
            }
            else if (j < SpaceList[i])
            {
                dp[i, j] = dp[i - 1, j];
            }
            else
            {
                dp[i, j] = Math.Max(dp[i - 1, j], dp[i - 1, j - SpaceList[i]] + ValueList[i]);
            }
        }
    }

    return dp[M - 1, N];
}

// 一维数组处理01背包问题
int Array1DKnapsack(int M, int N, int[] SpaceList, int[] ValueList)
{
    int[] dp = new int[N+1];
    for (int i = 0; i < M; i++)
    {
        for (int j = N; j >= SpaceList[i]; j--)
        {
            dp[j] = Math.Max(dp[j], dp[j - SpaceList[i]] + ValueList[i]);
        }
    }

    return dp[N];
}

int res = Array1DKnapsack(M, N, SpaceList, ValueList);
Console.WriteLine(res);